package binary_tree.LeetCode;
// class TreeNode {
//     int val;
//      TreeNode left;
//      TreeNode right;TreeNode() {}
//      TreeNode(int val) { this.val = val; }
//      TreeNode(int val, TreeNode left, TreeNode right) {
//          this.val = val;
//          this.left = left;
//          this.right = right;
//      }
//  }
public class Num110 {
    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        int leftHeight=height(root.left);
        int rightHeight=height(root.right);
        int abs=Math.abs(leftHeight-rightHeight);
        if(abs>1){
            return false;
        }
        return isBalanced(root.left)&&isBalanced(root.right);
    }

    public int height(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return 1 + Math.max(height(root.left), height(root.right));
    }
}
